package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.EvaluationFileDao;
import cn.topevery.garden.entity.po.EvaluationFile;
import cn.topevery.garden.entity.query.EvaluationFileQuery;
import cn.topevery.garden.entity.vo.EvaluationFileVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.basis.runtime.UserRuntime;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 检查考评附件表
 *
 * @author code-generate
 * @date 2022-01-05
 */
@Repository
public class EvaluationFileDaoImpl extends BaseDaoImpl<EvaluationFile> implements EvaluationFileDao {
    @Override
    public <P extends DbCsp> List<EvaluationFile> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<EvaluationFileVO> listPage(EvaluationFileQuery query) {
        String table = "t_evaluation_file tef";

        String columns = "tef.*";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tef.c_id desc");
        sqlQueryMate
                .where(true, "tef.c_db_status", ScriptConditionEnum.Eq, 0)
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, EvaluationFileVO.class);
    }

    @Override
    public EvaluationFile getEntityByColumn(String key,String value){
        KeyValue<String> map=new KeyValue<>();
        map.key=key;
        map.value=value;
        return getEntityByColumnEqValue(map);
    }

    @Override
    public void deleteByLogId(Integer logId, Integer userId) {

        String sql = "update t_evaluation_file set c_db_status = 1  where c_log_id = '" + logId+"'";
        run(sql, CommandType.Text, dbCommand -> {
            dbCommand.executeNonQuery();
        });

    }

    @Override
    public List<EvaluationFile> getFileListByLogId(Integer logId) {
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT * from t_evaluation_file where c_log_id ="+ logId);

        AtomicReference<List<EvaluationFile>> vo = new AtomicReference<>(new ArrayList<>());
        run(sb.toString(), CommandType.Text, dbCommand -> {
            vo.set(dbCommand.executeToList(EvaluationFile.class));
        });

        return vo.get();
    }

    @Override
    public void saveFiles(List<EvaluationFile> fileList) {
        insert(UserRuntime.getSysUser().userId, fileList);
    }
}
